#include "png8cropfx.h"
#include "dib4cropctx.h"

namespace png8crop
{
	dib4cropctx::dib4cropctx(dib4source *image)
		:cropctx(image),
		scropbuf_alloc(NULL)
	{
	}

	dib4cropctx::~dib4cropctx(void)
	{
		if(scropbuf_alloc != NULL) { delete[] scropbuf_alloc; scropbuf_alloc=NULL; }
	}

	void dib4cropctx::load_context_dib4(FILE* mnufile)
	{
		_TCHAR linebuf[MAX_PATH_EXT];
		parsectx parse;
		parse.the_this = this;
		const int constvals[] = { 1, 2, 3, 4, 5 };
		parse.parser.register_const(_T("color_scheme"), &constvals[0], vt_dword);
		parse.parser.register_const(_T("posx"), &constvals[1], vt_dword);
		parse.parser.register_const(_T("posy"), &constvals[2], vt_dword);
		parse.parser.register_const(_T("posw"), &constvals[3], vt_dword);
		parse.parser.register_const(_T("posh"), &constvals[4], vt_dword);

		parse.parser.register_variable(_T("::menued::numbuts"), &parse.numbuts, vt_dword, NULL, on_numbuts_got, &parse);
		parse.parser.register_variable(_T("::menued::numgroups"), &parse.numgroups, vt_dword, NULL, on_numbuts_got, &parse);


		while(!feof(mnufile))
		{
			_fgetts(linebuf, MAX_PATH_EXT, mnufile);
			try
			{
				parse.parser.parse_line(linebuf);
			}
			catch(ex_generic &ex)
			{
				ex.print_msg();
			}
		}

		int planeh = this->get_dib4image()->get_plane_size().h;
		int imgtype = this->get_dib4image()->get_ratio_type();
		int groupid = 0;

		for(int i=0; i < parse.numgroups; ++i)
		{
			if(parse.grouptypebuf_alloc[i] == imgtype)
			{
				groupid = i;
				break;
			}
		}
		this->num_scrops = parse.numbuts;
		this->scrops = this->scropbuf_alloc + this->num_scrops * groupid;
		
		this->get_dib4image()->update_crop_scheme(this->scrops, this->num_scrops);
		
		this->num_crops = this->num_scrops * 2;
		this->crops = this->cropbuf_alloc = new crop[this->num_crops];
		for(int i = 0; i < this->num_scrops; ++i)
		{
			this->crops[i] = this->scrops[i].crop;
			crop &actcrop = this->crops[i + this->num_scrops] = this->scrops[i].crop;
			actcrop.y += planeh;
		}

		if(parse.grouptypebuf_alloc != NULL)
		{
			delete[] parse.grouptypebuf_alloc;
			parse.grouptypebuf_alloc = NULL;
		}
	}

	void dib4cropctx::on_numbuts_got(const void *value, const void *ctx)
	{
		parsectx *context = (parsectx *) ctx;

		if(value == &context->numgroups)
		{
			if(context->numgroups > 0)
			{
				context->grouptypebuf_alloc = new int[context->numgroups];

				context->grouptype_diams[0].bytes = 4; 
				context->grouptype_diams[0].scales = context->numgroups;
				context->grouptype_diamlist.diam_count = 1;
				context->grouptype_diamlist.diams = context->grouptype_diams;
				context->parser.register_variable(_T("::menued::grouptype"), context->grouptypebuf_alloc, vt_dword,
					&context->grouptype_diamlist, on_numbuts_got, context);
			}
		}
		
		if(value == &context->numbuts || value == &context->numgroups)
		{
			if(context->numbuts == 0 || context->numgroups == 0)
			{
				return;
			}

			context->the_this->scropbuf_alloc = new schemed_crop[context->numbuts * context->numgroups];

			context->numbuts_diams[0].bytes = 4;
			context->numbuts_diams[0].scales = 5;
			context->numbuts_diams[1].bytes = 4 * 5 * context->numbuts;
			context->numbuts_diams[1].scales = context->numgroups;
			context->numbuts_diams[2].bytes = 4 * 5;
			context->numbuts_diams[2].scales = context->numbuts;
			context->numbuts_diamlist.diam_count = 3;
			context->numbuts_diamlist.diams = context->numbuts_diams;

			context->parser.register_variable(
				_T("::menued::but"),
				context->the_this->scropbuf_alloc,
				vt_dword,
				&context->numbuts_diamlist,
				NULL,
				context
				);
		}
		else
		{
			return;
		}
	}

}